package com.lebaduc.userAttribute;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.security.KeyStore.Entry;
import java.util.Scanner;
import java.util.TreeMap;

public class ExtractAtribute {

	/**
	 * @param args
	 */

	static void WritetoFile(int arr[], String filename) {

		try {
			// delete file if exists
			File f = new File("C:\\Users\\sev_user\\Desktop\\output matrix\\"
					+ filename);
			if (f.exists()) {
				f.delete();
			}

			FileWriter fw = new FileWriter(
					"C:\\Users\\sev_user\\Desktop\\output matrix\\" + filename,
					true);
			BufferedWriter bw = new BufferedWriter(fw);
			for (int i = 0; i < arr.length; i++) {
				bw.write(arr[i] + " ");
			}
			bw.newLine();
			bw.flush();

		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	static void resetOccupation(int occupation[]) {
		for (int i = 0; i < occupation.length; i++) {
			occupation[i] = 0;
		}
	}

	static void resetAgeArray(int ages[]) {
		for (int i = 0; i < ages.length; i++) {
			ages[i] = 0;
		}
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String[] columns = null;
		int count = 0;

		int size_age_arr = 0;
		String line = null;
		int[] temp = new int[2];
		int[] occupation = new int[21];
		int[] ages = new int[61];
		int age;
		TreeMap<String, Integer> temp_user_ages = new TreeMap<String, Integer>();
		TreeMap<String, Integer> user_ages = new TreeMap<String, Integer>();
		FileReader fr;
		try {
			fr = new FileReader("C:\\Users\\sev_user\\Desktop\\ml-100k\\u.user");
			BufferedReader br = new BufferedReader(fr);
			while ((line = br.readLine()) != null) {
				columns = line.split("\\|");
				// =================================================================================================================
				// tao ma tran gender
				if (columns[2].equalsIgnoreCase("M")) {
					temp[0] = 1;
					temp[1] = 0;
				} else {
					temp[0] = 0;
					temp[1] = 1;
				}
				WritetoFile(temp, "u.gender");

				// =================================================================================================================
				// === tao ma tra occupation
				resetOccupation(occupation);
				String occputations = columns[3];

				if (occputations.equalsIgnoreCase("administrator")) {
					occupation[0] = 1;
				}
				if (occputations.equalsIgnoreCase("artist")) {
					occupation[1] = 1;
				}

				if (occputations.equalsIgnoreCase("doctor")) {
					occupation[2] = 1;
				}

				if (occputations.equalsIgnoreCase("educator")) {
					occupation[3] = 1;
				}

				if (occputations.equalsIgnoreCase("engineer")) {
					occupation[4] = 1;
				}

				if (occputations.equalsIgnoreCase("entertainment")) {
					occupation[5] = 1;
				}

				if (occputations.equalsIgnoreCase("executive")) {
					occupation[6] = 1;
				}

				if (occputations.equalsIgnoreCase("healthcare")) {
					occupation[7] = 1;
				}

				if (occputations.equalsIgnoreCase("homemaker")) {
					occupation[8] = 1;
				}

				if (occputations.equalsIgnoreCase("lawyer")) {
					occupation[9] = 1;
				}

				if (occputations.equalsIgnoreCase("librarian")) {
					occupation[10] = 1;
				}

				if (occputations.equalsIgnoreCase("marketing")) {
					occupation[11] = 1;
				}

				if (occputations.equalsIgnoreCase("none")) {
					occupation[12] = 1;
				}

				if (occputations.equalsIgnoreCase("other")) {
					occupation[13] = 1;
				}

				if (occputations.equalsIgnoreCase("programmer")) {
					occupation[14] = 1;
				}

				if (occputations.equalsIgnoreCase("retired")) {
					occupation[15] = 1;
				}

				if (occputations.equalsIgnoreCase("salesman")) {
					occupation[16] = 1;
				}

				if (occputations.equalsIgnoreCase("scientist")) {
					occupation[17] = 1;
				}

				if (occputations.equalsIgnoreCase("student")) {
					occupation[18] = 1;
				}

				if (occputations.equalsIgnoreCase("technician")) {
					occupation[19] = 1;
				}

				if (occputations.equalsIgnoreCase("writer")) {
					occupation[20] = 1;
				}

				WritetoFile(occupation, "u.occupation");

				// =================================================================================================================
				// // Create tree map age to calculate how many age of user we
				// have ;
				age = Integer.parseInt(columns[1]);
				temp_user_ages.put(columns[1], count++);

			}
			br.close();

			System.out.println(temp_user_ages.size());

			// == Create tree map user age with value number that ascending
			for (java.util.Map.Entry<String, Integer> child : temp_user_ages
					.entrySet()) {
				user_ages.put(child.getKey(), size_age_arr++);

			}
			//
			// for (java.util.Map.Entry<String, Integer> child :user_ages
			// .entrySet()) {
			// System.out.println(child.getKey()+"  "+ child.getValue() );
			// //user_ages.put(child.getKey(), count++);
			//
			// }

		} catch (Exception e) {
			e.printStackTrace();
		}
		// === Write user ages to file
		try {
			fr = new FileReader("C:\\Users\\sev_user\\Desktop\\ml-100k\\u.user");
			BufferedReader br = new BufferedReader(fr);
			while ((line = br.readLine()) != null) {
				columns = line.split("\\|");
				String my_age = columns[1];
				resetAgeArray(ages);
				for (java.util.Map.Entry<String, Integer> child : user_ages
						.entrySet()) {
					if (my_age.equalsIgnoreCase(child.getKey())) {
						ages[child.getValue()] = 1;
						break;
					}

				}
				WritetoFile(ages, "u.age");
			}
			br.close();
		} catch (Exception e) {
			// TODO: handle exception
		}

	}

}
